/*
 * @Author: 晟松
 * @Date: 2021-12-10 22:28:09
 * @LastEditors: 晟松
 * @LastEditTime: 2022-02-26 12:04:16
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\src\main\controller\VolController.js
 * @Description: 志愿服务的一些操作
 */

import Router from "koa-router";
import koaBodyparser from "koa-bodyparser";
import VolunteerService from "@main/service/VolService";
import allowUserIdentity, { isSchoolAdmin } from "@main/middleware/routeMid/validateIdentity";
import { LoginError } from "@main/common/CommonError";

let volRouter = new Router({
    prefix: "/api/vol",
});
/**
 * @description: 居民发布志愿服务，后面可能可以让学生也可以发布
 * @param {*}
 * @return {*}
 */
volRouter.post("/add", koaBodyparser(), allowUserIdentity(["resident", "student"]), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.add(params));
});
volRouter.post("/delete", koaBodyparser(), allowUserIdentity(["resident", "student"]), async (ctx) => {
    let params = ctx.request.body;
    let user = ctx.state.user;
    console.log("params: ", params, user);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.delete(params, user));
});
/**
 * @description: 获得用户的所有志愿服务信息，只能是学生或居民
 * @param {*}
 * @return {*}
 */
volRouter.post("/getUserAllVol", koaBodyparser(), allowUserIdentity(["resident", "student"]), async (ctx) => {
    let params = ctx.request.body;
    // 通过token直接获取用户信息
    // let params = ctx.state.user
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.getUserAllVol(params));
});
/**
 * @description: 获取单个志愿服务信息，所有人都可以
 * @param {*}
 * @return {*}
 */
volRouter.post("/getVolInfo", koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.getVolInfo(params));
});
/**
 * @description: 微信小程序获取首页的志愿服务列表，可带limit和offset
 * @param {*}
 * @return {*}
 */
volRouter.get("/getVolList", koaBodyparser(), async (ctx) => {
    // let params = ctx.request.body;
    let query = ctx.request.query;
    console.log("query: ", query);
    // console.log("params: ", params,);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.getVolList(query));
});

/**
 * @description: 更新一个志愿服务的信息
 * @param {*}
 * @return {*}
 */
volRouter.post("/update", koaBodyparser(), allowUserIdentity(["resident", "student"]), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.update(params));
});
/**
 * @description: 居委会和辅导员审核一个居民的志愿服务，可有两种反馈，一种是审核通过，一种是被打回
 * @param {*}
 * @return {*}
 */
volRouter.post("/reviewVol", allowUserIdentity(["community", "school"]), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.reviewVol(params));
});
/**
 * @description: 学生或居民领取志愿服务
 * @param {*}
 * @return {*}
 */
volRouter.post("/receiveVol", allowUserIdentity(["student", "resident"]), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.receiveVol(params));
});

/**
 * @description: 院级管理员获得自己学生的所有志愿服务记录
 * @param {*}
 * @return {*}
 */
volRouter.post(
    "/getCollegeAllStudentVol",
    // allowUserIdentity("school"),
    koaBodyparser(),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(await VolunteerService.getCollegeAllStudentVol(params));
    }
);

/**
 * @description: 院级管理员查询满足条件的自己学生的所有志愿服务记录
 * @param {*}
 * @return {*}
 */
volRouter.post(
    "/searchCollegeAllStudentVol",
    // allowUserIdentity("school"),
    koaBodyparser(),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(await VolunteerService.searchCollegeAllStudentVol(params));
    }
);

/**
 * @description: 校级管理员获得自己学生的所有志愿服务记录
 * @param {*}
 * @return {*}
 */
volRouter.post(
    "/getAllStudentVol",
    // allowUserIdentity("school"),
    koaBodyparser(),
    async (ctx) => {
        let params = ctx.request.body;
        console.log("params: ", params);
        ctx.validateParams.validateParamId(params);

        ctx.body = ctx.renderJson(await VolunteerService.getAllStudentVol(params));
    }
);
/**
 * @description: 居委会获得自己居民的所有志愿服务
 * @param {*}
 * @return {*}
 */
volRouter.post("/getAllResidentVol", allowUserIdentity("community"), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.getAllResidentVol(params));
});

/**
 * @description: 居委会查询满足条件的自己居民的所有志愿服务
 * @param {*}
 * @return {*}
 */
volRouter.post("/searchAllResidentVol", allowUserIdentity("community"), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.searchAllResidentVol(params));
});
/**
 * @description: 接受者完成请求发起反馈
 * @param {*}
 * @return {*}
 */
volRouter.post("/feedbackVol", allowUserIdentity(["resident", "student"]), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.feedbackVol(params));
});
/**
 * @description: 发起者评价一个志愿服务
 * @param {*}
 * @return {*}
 */
volRouter.post("/evaluateVol", allowUserIdentity(["resident", "student"]), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.evaluateVol(params));
});

/**
 * @description: 模糊的查询符合条件的志愿服务，必须是状态为等待领取的状态
 * @param {*}
 * @return {*}
 */
volRouter.post("/searchVol", allowUserIdentity(["resident", "student"]), koaBodyparser(), async (ctx) => {
    let params = ctx.request.body;
    console.log("params: ", params);
    // ctx.validateParams.validateParamId(params);

    ctx.body = ctx.renderJson(await VolunteerService.searchVol(params));
});

export default volRouter;
